//
//  ShareQQ.swift
//  BMWShareSDK
//
//  Created by Li Shengbing, (Shengbing.Li@partner.bmw.com) on 2019/6/11.
//  Copyright © 2019年 Li Shengbing, (Shengbing.Li@partner.bmw.com). All rights reserved.
//  /// 回调冲突OC解决办法：
//  https://www.jianshu.com/p/c83e7d4acb21
//  qq- https://blog.csdn.net/mo_xiao_mo/article/details/52618514

// https://www.jianshu.com/p/581edba36b5b
// 可以结合 + 文档一起分享



import UIKit

class ShareQQ: NSObject {
    
    public static let shareInstance: ShareQQ = ShareQQ()

    
    static func share(type: ShareQQType, platform: ShareQQPlatform? = ShareQQPlatform.qqAnyWhere, shareDestType: ShareDestType? = ShareDestTypeQQ) {
        
        var tempReq = SendMessageToQQReq()
        switch type {
            /// Share text to QQ
        case .textOnly(text: let _text):
            let textObj = QQApiTextObject(text: _text)
            textObj?.shareDestType = shareDestType == nil ? ShareDestTypeQQ : shareDestType!
            textObj?.cflag = platform == nil ? ShareQQPlatform.qqAnyWhere.description : platform!.description
            tempReq = SendMessageToQQReq(content: textObj)
            
            
            /// Share image to QQ
        case .imageOnly(originImage: let _originImage, previewImage: let _previewImage, text: let _text, description: let _description):
            let originImageData = _originImage?.pngData()
            let thumbImageData = _previewImage?.jpegData(compressionQuality: 0.8)
            let imgObj = QQApiImageObject(data: originImageData, previewImageData: thumbImageData, title: _text ?? "", description: _description ?? "")
            imgObj?.shareDestType = shareDestType == nil ? ShareDestTypeQQ : shareDestType!
            imgObj?.cflag = platform == nil ? ShareQQPlatform.qqAnyWhere.description : platform!.description
            tempReq = SendMessageToQQReq(content: imgObj)
            
            /// Share URL to QQ
        case .urlNews(url: let _url, previewImageUrl: let _previewImageUrl, text: let  _text, description: let _description):
            let url = URL(string: _url ?? "")
            let previewImageUrl = URL(string: _previewImageUrl ?? "")
            
            let urlObj = QQApiURLObject(url: url, title: _text ?? "", description: _description ?? "", previewImageURL: previewImageUrl, targetContentType: QQApiURLTargetTypeNews)
            tempReq = SendMessageToQQReq(content: urlObj)
            
            /// Share Audio to QQ
        case .audio(audioUrl: let _audioUrl, audioPreviewImageUrl: let _audioPreviewImageUrl, audioPreviewImage: let _audioPreviewImage, text: let _text, description: let _description):
            let audioUrl = URL(string: _audioUrl ?? "")
            let audioPreviewImageUrl = URL(string: _audioPreviewImageUrl ?? "")
            if _audioPreviewImage == nil {
                let urlObj = QQApiAudioObject(url: audioUrl, title: _text ?? "", description: _description ?? "", previewImageURL: audioPreviewImageUrl, targetContentType: QQApiURLTargetTypeAudio)
                tempReq = SendMessageToQQReq(content: urlObj)
            }else {
                let urlObj = QQApiAudioObject(url: audioUrl, title: _text, description: _description, previewImageData: _audioPreviewImage?.jpegData(compressionQuality: 0.7), targetContentType: QQApiURLTargetTypeAudio)
                tempReq = SendMessageToQQReq(content: urlObj)
            }
            
            
            /// Share Vedio to QQ
        case .video(videoUrl: let _videoUrl, videoPreviewImageUrl: let _videoPreviewImageUrl, videoPreviewImage: let _videoPreviewImage,  text: let _text, description: let _description):
            let vedioUrl = URL(string: _videoUrl ?? "")
            let videoPreviewImageUrl = URL(string: _videoPreviewImageUrl ?? "")
            if _videoPreviewImage == nil {
                let urlObj = QQApiVideoObject(url: vedioUrl, title: _text ?? "", description: _description ?? "", previewImageURL: videoPreviewImageUrl, targetContentType: QQApiURLTargetTypeVideo)
                tempReq = SendMessageToQQReq(content: urlObj)
            }else {
                let urlObj = QQApiVideoObject(url: vedioUrl, title: _text, description: _description, previewImageData: _videoPreviewImage?.jpegData(compressionQuality: 0.7), targetContentType: QQApiURLTargetTypeVideo)
                tempReq = SendMessageToQQReq(content: urlObj)
            }
            
        case .file(text: let _text, description: let _description, previewImage: let _previewImage, data: let _data):
            let fileObj = QQApiFileObject(data: _data, previewImageData: _previewImage?.jpegData(compressionQuality: 0.7), title: _text, description: _description)
            tempReq = SendMessageToQQReq(content: fileObj)
        }
        _ = QQApiInterface.send(tempReq)
    }
}

extension ShareQQ: QQApiInterfaceDelegate {
    
    func onResp(_ resp: QQBaseResp!) {
        if resp.isKind(of: SendMessageToQQResp.self) {
            let tempResp = resp as! SendMessageToQQResp
            
            /// -4: 取消分享 0: 分享成功
            var result = ""
            if UInt(tempResp.type) == ESENDMESSAGETOQQRESPTYPE.rawValue {
                if tempResp.result == "0" {
                    result = BMWNSLocalizedString.localizedString("SHARE_QQ_CALLBACK_SUCCESS", comment: "Shared")
                }else if tempResp.result == "-4" {
                    result = BMWNSLocalizedString.localizedString("SHARE_QQ_CALLBACK_USERCANCEL", comment: "Cancelled to share")
                }else {
                    result = BMWNSLocalizedString.localizedString("SHARE_QQ_CALLBACK_FAIL", comment: "Failed to share")
                }
            }
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25, execute: {
                if BMWShareManager.shareComplete != nil {
                    BMWShareManager.shareComplete!(result)
                }
            })
        }
    }
    
    func onReq(_ req: QQBaseReq!) {
    }
    
    func isOnlineResponse(_ response: [AnyHashable : Any]!) {
    }
}

public enum ShareQQType {
    /// text
    case textOnly(text: String)
    
    /// image
    case imageOnly(originImage: UIImage?, previewImage: UIImage?, text: String?, description: String?)
    
    /// url
    case urlNews(url: String?, previewImageUrl: String?, text: String?, description: String?)
    
    /// audio
    case audio(audioUrl: String?, audioPreviewImageUrl: String?, audioPreviewImage: UIImage?, text: String?, description: String?)
    
    /// vedio
    case video(videoUrl: String?, videoPreviewImageUrl: String?, videoPreviewImage: UIImage?, text: String?, description: String?)
    
    /// file
    case file(text: String?, description: String?, previewImage: UIImage?, data: Data)
}

public enum ShareQQPlatform {
    /// Only share to zone
    case zoneOnly
    
    /// Forbid share to zone
    case firbidZone
    
    /// Share qq, can select Computer、Zone、Contacts、Favirites etc.
    case qqAnyWhere
    
    /// Only share text or image to favirites.
    case favirites
    
    /// Only share text or image to computer
    case dataline
    
    var description: UInt64 {
        switch self {
        case .qqAnyWhere:
            return UInt64(kQQAPICtrlFlagQQShare)
            
        case .firbidZone:
            return UInt64(kQQAPICtrlFlagQZoneShareForbid)
            
        case .zoneOnly:
            return UInt64(kQQAPICtrlFlagQZoneShareOnStart)
            
        case .favirites:
            return UInt64(kQQAPICtrlFlagQQShareFavorites)
            
        case .dataline:
            return UInt64(kQQAPICtrlFlagQQShareDataline)
        }
    }
}


